/*
 * instrument definition
 *
 * Choi Jonghwan <saxboy@gmail.com>
 * 2006/04/24
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "instrument.h"

INSTRUMENT instrument_New(void)
{
	INSTRUMENT i=NULL; 

	if ( (i=(INSTRUMENT)malloc(sizeof(_INSTRUMENT)))!=NULL)
	{
		memset(i, 0, sizeof(_INSTRUMENT)); 
	}
	return i; 
}

INSTRUMENT instrument_NewWithName(const char *name)
{
	INSTRUMENT i=NULL; 
	if ( (i = instrument_New() )!=NULL)
	{
		if ( instrument_Load(i, name) > 0 )
			return i; 
	}
	return NULL; 
}

int instrument_Delete(INSTRUMENT i)
{
	if (i==NULL) return -1; 
	free(i); 
	return 1; 
}



_INSTRUMENT g_instrument_array[] = 
{
	{"bass", -2, 33, 1 }, 
	{"piano", 0, 1, 1 }, 
	{"saxophone", 0, 66, 0 }, 
	{"drum", 0, 0, 0 }, 	/* dummy... */
	{NULL, -1, -1, 0}
}; 

int instrument_Load(INSTRUMENT inst, const char *name)
{
	int i=0; 

	for (i=0; g_instrument_array[i].name!=NULL; i++)
	{
		if ( strcmp( name, g_instrument_array[i].name) == 0 )
		{
			INSTRUMENT cur_inst =  &g_instrument_array[i]; 

			inst->name = cur_inst->name; 
			inst->relative_octave = cur_inst->relative_octave; 
			inst->midi_program = cur_inst->midi_program; 
			inst->b_is_polyphonic = cur_inst->b_is_polyphonic; 

			return 1; 
		}
	}
	return -1; 
}

